package co.fitstart.admin.dao;

import java.util.Date;
import java.util.List;

import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.persistence.annotation.Execute;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;

import co.fitstart.admin.entity.report.HourlySchedule;
import co.fitstart.entity.coach.CoachRestCalendar;
import co.fitstart.entity.coach.CoachRestType;
import co.fitstart.entity.principal.Principal;

@Dynamic("hibernate")
public interface CoachRestCalendarDao {
	
	/**
	 * 
	 * @param beginDate
	 * @param endDate
	 * @param coachIds
	 * @return
	 */
	@Query("SELECT new co.fitstart.admin.entity.report.HourlySchedule(cr.restDate, cr.coach.name, cr.restHour, cr.coachRestType) FROM CoachRestCalendar cr WHERE cr.disabled =false AND cr.restDate >= :beginDate AND cr.restDate <= :endDate AND cr.coach.id in (:coachIds) ORDER BY cr.restDate ASC, cr.restHour ASC")
	public List<HourlySchedule> listRestSchedule(@Variable("beginDate")Date beginDate, @Variable("endDate")Date endDate, @Variable("coachIds")List<Long> coachIds);
    
    /**
     * 
     * @param coach
     * @param date
     * @return
     */
    @Query("FROM CoachRestCalendar WHERE disabled = false AND coach = :coach AND restDate = :date")
    public List<CoachRestCalendar> list(@Variable("coach") Principal coach, @Variable("date") Date date);
    
    /**
     * 
     * @param coach
     * @param beginDate
     * @param endDate
     * @return
     */
    @Query("FROM CoachRestCalendar WHERE disabled = false AND coach = :coach AND coachRestType != 4 AND restDate BETWEEN :beginDate AND :endDate")
    public List<CoachRestCalendar> list(@Variable("coach") Principal coach, @Variable("beginDate") Date beginDate, @Variable("endDate") Date endDate);
    
    /**
     * 
     * @param coach
     * @param date
     * @param hours
     * @return
     */
    @Query("FROM CoachRestCalendar coachRestCalendar WHERE  coachRestCalendar.coachRestType in (0,1,3,4,5,6) AND coachRestCalendar.disabled = false AND coachRestCalendar.coach = :coach AND coachRestCalendar.restDate = :date AND coachRestCalendar.restHour in ( :hours )")
    public List<CoachRestCalendar> list(@Variable("coach") Principal coach, @Variable("date") Date date, @Variable("hours") List<String> hours);
    
    /**
     * 
     * @param coach
     * @param date
     * @param hours
     * @return
     */
    @Query("FROM CoachRestCalendar coachRestCalendar WHERE coachRestCalendar.disabled = false AND coachRestCalendar.coachRestType != 2 AND coachRestCalendar.coach = :coach AND coachRestCalendar.restDate = :date AND coachRestCalendar.restHour in ( :hours )")
    public List<CoachRestCalendar> list(@Variable("coach") Principal coach, @Variable("date") Date date, @Variable("hours") String[] hours);
    
    /**
     * 
     * @param coach
     * @param date
     * @return
     */
    @Query("SELECT coachRestCalendar.restHour FROM CoachRestCalendar coachRestCalendar WHERE coachRestCalendar.disabled = false AND coachRestCalendar.coach = :coach AND coachRestCalendar.restDate = :date")
    public List<String> listRestHours(@Variable("coach") Principal coach, @Variable("date") Date date);
    
    /**
     * 
     * @param coach
     * @return
     */
    @Query("FROM CoachRestCalendar WHERE disabled = false AND coach = :coach AND restDate = :restDate AND restHour = :restHour AND coachRestType = :type")
    public CoachRestCalendar loadCoachRestCalendar(@Variable("coach") Principal coach, @Variable("restDate") Date restDate, @Variable("restHour") String restHour, @Variable("type") CoachRestType type);
    
    /**
     * 
     * @param coach
     * @param beginDate
     * @param endDate
     */
    @Execute("UPDATE CoachRestCalendar calendar SET calendar.disabled = true WHERE calendar.coach = :coach AND calendar.coachRestType = 4 AND calendar.restDate BETWEEN :beginDate AND :endDate")
    public void disableRepeatAnnualLeave(@Variable("coach") Principal coach, @Variable("beginDate") Date beginDate, @Variable("endDate") Date endDate);

}
